// pages/my/logon/guide/guide.js
var LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startAn: true,
    userInfo: {},
    isExist: false,
    codeInfo: {}

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.userInfo" 这个 scope
    let _this = this;
    wx.getSetting({
      success(res) {
        if (!res['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              wx.getUserInfo({
                success: function (res) {
                  _this.setData({ isExist: true });
                  wx.setStorage({ key: "wx_userInfo", data: res.userInfo });
                },
                fail: function (ex) {
                  //扫码禁用
                  _this.setData({ isExist: false });
                }
              });
            }
          })
        }
      }
    })
  },
  sleep: function (numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
      now = new Date();
      if (now.getTime() > exitTime) {
        return;
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  ,
  loginEntrance: function () {
    var userInfo = LApp.globalData.userInfo;
    if (userInfo) {
      if (userInfo.Type == 1 || userInfo.Type == 0) {
        if (userInfo.RoleID.searchOf("Ox1") ||
          userInfo.RoleID.searchOf("Ox2") ||
          userInfo.RoleID.searchOf("Ox3") ||
          userInfo.RoleID.searchOf("Ox4") ||
          userInfo.RoleID.searchOf("Ox5") ||
          userInfo.RoleID.searchOf("Ox6") ||
          userInfo.RoleID.searchOf("Ox7") ||
          userInfo.RoleID.searchOf("Ox31") ||
          userInfo.RoleID.searchOf("Ox32") ||
          userInfo.RoleID.searchOf("Ox33") ||
          userInfo.RoleID.searchOf("Ox35") ||
          userInfo.RoleID.searchOf("Ox36") ||
          userInfo.RoleID.searchOf("Ox37")) {
          wx.switchTab({
            url: '../../../operat/operat',
          })
        } else if (userInfo.RoleID.searchOf("Ox8")) {
          wx.navigateTo({
            url: '../../../operat/fleedTools/fleedTools',
          })
        } else if (userInfo.RoleID.searchOf("Ox11") ||
          userInfo.RoleID.searchOf("Ox12") ||
          userInfo.RoleID.searchOf("Ox13")) {
          wx.navigateTo({
            url: '../../../operat/origins/origins',
          })
        }
      } else if (userInfo.Type == 2) {
        wx.navigateTo({
          url: '../loginEntrance/loginEntrance?_loginName =' + userInfo.LoginName + ' & _pwd=' + userInfo.Pwd + '&_status=true',
        })
      } else {
        wx.redirectTo({
          url: '../loginEntrance/loginEntrance',
        })
      }
    } else {
      wx.redirectTo({
        url: '../loginEntrance/loginEntrance',
      })
    }
  },
  startScan: function () {
    let _this = this;
    if (this.data.isExist == false) {
      wx.showModal({
        title: '提示',
        content: '你尚未授权',
      })
      return
    }
    wx.scanCode({
      onlyFromCamera: true,
      success: function (res) {
        var _url = res.result;
        var code = _this.GetUrlParam(res.result, "code");
        LApp._get({
          url: 'nbapi/codemanager/code/search',
          data: { code: code },
          success: function (res) {
            if (res.CodeType.indexOf("2"))//溯源码
            {
              _this.OriginsSearch(_url);
            }
            else {//防伪
              _this.FWSearch(res);
            }
          },
          fail: function (e) {
            debugger
            wx.navigateTo({
              url: 'error/error',
            })
          }
        })
      },
      fail: function (res) { },
    })
  },
  OriginsSearch: function (_url) {
    this._WebUrl = _url;
    LApp.pages.add(this);
    wx.navigateTo({
      url: 'origins/origins?url=' + _url,
    })
  },
  FWSearch: function (model) {
    let _url = '';
    if (model.QueryCount == 1) {
      _url = "correct/correct";
    }
    else {
      _url = "doubt/doubt";
    }
    this.setData({ codeInfo: model })
    LApp.pages.add(this);
    wx.navigateTo({
      url: _url,
    })
  },
  //获取Url中指定参数
  GetUrlParam: function (source, conditionName) {
    if (source.indexOf("http") == -1) return source;
    var reg = new RegExp("(^|&)" + conditionName + "=([^&]*)(&|$)");
    var r = source.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    var _temp = source.split("code=");
    if (_temp == null) return null;
    if (_temp.length <= 1) return null;
    var _s_t = _temp[1];
    if (_s_t.indexOf("&")) {
      return _s_t.split("&")[0];
    }
    return _s_t;
  }
})